package io.externalize;

import java.io.Externalizable;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;

class Employee implements Externalizable {
	private int id;
	private String name;

	public Employee() {
	}

	public Employee(int i, String s) {
		this.id = i;
		this.name = s;
	}

	public String toString() {
		return "ID: " + this.id + "\nName: " + this.name;
	}

	public void readExternal(ObjectInput in) throws IOException,
			ClassNotFoundException {
		id = in.readInt();
		name = in.readUTF();
	}

	public void writeExternal(ObjectOutput out) throws IOException {
		out.writeInt(id);
		out.writeUTF(name);
	}
}

public class IOExternalization {
	public static void main(String args[]) throws IOException,
			ClassNotFoundException {
		// EXTERNALIZATION
		Employee e = new Employee(10, "Super");
		FileOutputStream fos = new FileOutputStream("object.dat");
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		oos.writeObject(e);
		System.out.println("object Serialization..");

		// DE-EXTERNALIZATION
		FileInputStream fis = new FileInputStream("object.dat");
		ObjectInputStream ois = new ObjectInputStream(fis);
		Object obj = ois.readObject();
		Employee ee = (Employee) obj;
		System.out.println(ee);
	}
}